package com.yuandian.management.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.management.entity.ConfReturnCode;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.log.annotation.SysLog;
import com.yuandian.management.service.ConfReturnCodeService;
import com.yuandian.management.vo.CodeDetailsExcelVo;
import com.yuandian.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 返回码配置表-Controller
 * @author zfsn
 * @date 2023-3-28
 */
@Api(tags = "外部指标管理-导入导出管理-返回码配置管理")
@RestController
@RequestMapping("/returnCode")
@Deprecated
public class ConfReturnCodeController {

	@Autowired
	private ConfReturnCodeService confReturnCodeService;

	/**
	 * 通过主键查询数据
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键查询数据", notes = "通过主键查询数据，支持批量查询")
	@GetMapping("/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "id(支持多选)", paramType = "query", required = true, dataTypeClass = Integer.class)
	})
	public R<Page<ConfReturnCode>> queryByIds(@PathVariable("ids") List<Integer> idList) {
		ConfReturnCode confReturnCode = new ConfReturnCode();
		confReturnCode.setIds(new ArrayList<>(idList));
		return R.ok(confReturnCodeService.listPage(confReturnCode));
	}

	/**
	 * 分页查询
	 * @param confReturnCode 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "返回码名称(支持模糊查询)", paramType = "body", required = true, dataType = "String"),
	})
	public R<Page<ConfReturnCode>> listPage(@RequestBody @ApiIgnore ConfReturnCode confReturnCode) {
		return R.ok(confReturnCodeService.listPage(confReturnCode));
	}

	/**
	 * 新增数据
	 * @param confReturnCode 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增数据", notes = "新增数据")
	@SysLog("新增返回码配置表数据")
	@PostMapping("/save")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "返回码名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "typeId", value = "返回码类型id，对应字典类型：return_code_type", paramType = "body", required = true, dataTypeClass = Integer.class),
	})
	public R<String> saveReturnCode(@RequestBody @ApiIgnore ConfReturnCode confReturnCode) {
		return confReturnCodeService.saveReturnCode(confReturnCode);
	}

	/**
	 * 通过主键更新数据
	 * @param confReturnCode 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键更新数据", notes = "通过主键更新数据")
	@SysLog("更新返回码配置表数据")
	@PutMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "name", value = "返回码名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "typeId", value = "返回码类型id，对应字典类型：return_code_type", paramType = "body", required = true, dataTypeClass = Integer.class),
	})
	public R<String> updateById(@RequestBody @ApiIgnore ConfReturnCode confReturnCode) {
		return confReturnCodeService.updateReturnCodeById(confReturnCode);
	}

	/**
	 * 通过主键删除数据
	 * @param ids 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键删除数据", notes = "通过主键删除数据")
	@SysLog("删除返回码配置表数据")
	@DeleteMapping("/delete/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "id(支持多选)", paramType = "query", required = true, dataTypeClass = Integer.class)
	})
	public R<Boolean> removeById(@PathVariable("ids") List<Integer>  ids){
		return R.ok(confReturnCodeService.removeByIds(ids));
	}


	/**
	 * 获取返回码信息
	 * @param confReturnCode 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "获取返回码信息", notes = "获取返回码信息")
	@PostMapping("/getCodeDetail")
	public R<List<ConfReturnCode>> getCodeDetail(@RequestBody ConfReturnCode confReturnCode) {
		return R.ok(confReturnCodeService.getCodeDetail(confReturnCode));
	}

	/**
	 * 下载专返回码导入模板
	 * @param response 响应上下文
	 */
	@ApiOperation(value = "下载返回码导入模板", notes = "下载返回码导入模板")
	@GetMapping("/importTemplate")
	public void importTemplate(HttpServletResponse response) {
		try {
			confReturnCodeService.importTemplate(response);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	/**
	 * 上传
	 * @param file file
	 * @return Result
	 * @throws IOException
	 */
	@ApiOperation(value = "返回码导入", notes = "返回码导入")
	@PostMapping("/upload")
	public R upload(@RequestParam("file") MultipartFile file) throws IOException {
		Map<String, List<CodeDetailsExcelVo>> resultMap = ExcelUtil.readAndSaveExcelData(file, 0, 2, CodeDetailsExcelVo.class, confReturnCodeService);

		List<CodeDetailsExcelVo> successList = resultMap.get("result");
		List<CodeDetailsExcelVo> failList = resultMap.get("fail");
		if (!CollectionUtils.isEmpty(successList) && CollectionUtils.isEmpty(failList)) {
			return R.ok("导入完成，成功条数：" + successList.size() + ",失败条数：" + failList.size());
		}

		return R.failed("导入完成，成功条数：" + successList.size() + ",失败条数：" + failList.size());
	}
}
